<?php
/**
 * This file is part of Hyperf Mall.
 *
 * @link     https://gitee.com/scwlkj/hyperf_mall
 * @contact  hyperf_mall@163.com
 */

namespace App\Repository;


use App\Constants\Goods;
use App\Model\GoodsCategoryModel;
use Hyperf\Di\Annotation\Inject;


class GoodsCategoryRepository extends BaseRepository
{
    /**
     * @Inject
     * @var GoodsCategoryModel
     */
    protected $model;

    /**
     * Notes: 获取分类树
     * Author: Bruce.z
     * DateTime: 2021/12/29 10:16
     * @param int $depth 1|2|3
     * @return array
     */
    public function getTree(int $depth = Goods::CATE_THREE) : array
    {
        if($depth == Goods::CATE_TWO){
            return $this->nextTree(0);
        }else{
            $level = $this->_searchFactor(['pid'=>0])->get(['pid','name','image','sort','id','level','create_time','goods_num','status'])->toArray();
            if($depth == Goods::CATE_FIRST) return $level;

           foreach ($level as &$item){
               $item['children'] = $this->nextTree($item['id']);
           }

           return $level;
        }
    }

    private function nextTree($pid): array
    {
        $rows = $this->_searchFactor(['pid'=>$pid])->get(['pid','name','id','image','sort','level','create_time','goods_num','status'])->toArray();
        if(empty($rows)) return [];

        $ids = array_column($rows, 'id');
        $level_next = $this->_searchFactor(['pid'=>$ids])->get(['pid','name','sort','image','id','level','create_time','goods_num','status'])->toArray();

        foreach ($rows as &$v){
            foreach ($level_next as $value){
                if($v['id'] == $value['pid']){
                    $v['children'][] = $value;
                }
            }
        }

        return $rows;
    }

    /**
     * Notes:
     * Author: Bruce.z
     * DateTime: 2021/12/29 10:50
     * @param int $depth
     * @return array
     */
    public function makeSelectTree(int $depth = Goods::CATE_THREE) :array
    {
        $tree = $this->getTree($depth);

        return [[
            'id'=>0,
            'name'=>'顶级分类',
            'children'=>$tree
        ]];
    }

    /**
     * Notes: 添加
     * @param $param
     * @return \App\Model\BaseModel|\Hyperf\Database\Model\Model
     */
    public function addData($param)
    {
        if($param['pid'] != 0){
            $re = $this->getOne(['id'=>$param['pid']],['level']);
            $param['level'] = intval($re['level']) + 1;
        }

        return $this->baseAdd($param);
    }

    /**
     * Notes: 返回 封装 [pid, id] 格式
     * Author: Bruce.z
     * DateTime: 2022/2/15 16:14
     * @param $id
     * @return array
     */
    public function getParentID($id) :array
    {
        $re = $this->getOne(['id'=>$id],['id','pid']);
        if($re['pid'] == 0) return [$re['id']];

        $return = array_merge([$id], $this->getParentID($re['pid']));
        sort($return);
        return $return;
    }

    /**
     * Notes: 获取下级分类
     * Author: Bruce.z
     * DateTime: 2022/6/14 16:11
     * @param $id
     * @return array
     */
    public function getNextCategory($id):array
    {
        return $this->nextTree($id);
    }

    /**
     * Notes: 获取当前分类下的子分类字符串
     * Author: Bruce.z
     * DateTime: 2022/9/6 11:49
     * @param int $cId
     * @param string $cateIds
     * @return string
     */
    public function getCategoryIds(int $cId, string $cateIds = ''): string
    {
        $cateInfo = $this->getOne(['id'=>$cId], ['id', 'pid', 'name']);
        if (!empty($cateInfo)) {
            $cateIds = $cateInfo['id'] . ',' . $cateIds;
            $data = $this->getCategoryIds($cateInfo['pid'], $cateIds);
        } else {
            $data = $cateIds;
        }

        return rtrim($data, ',');
    }
}
